#include <stdio.h>
#include "serial.h"
#include "modbus.h"
#include <windows.h>
// Create serial port


int main(int argc, char **argv)
{
    serial ser;
    modbus mb(0x02);
    unsigned char buffer[256] = {0};
    int i;

    // Open serial port ("COM3", "/dev/ttyUSB0")
    int open=ser.Open("COM3", 9600, 8, NO, 1);
    printf("open=%d\n", open);
    Sleep(1000);
    while (1)
    {
        int length = mb.read_DI();
        unsigned char *data = mb.get_data();

        char ret = ser.Write(data, length); // 发送包含CRC校验值的数据

        printf("main ret=%d\n", ret);

        // Wait character
        length = ser.Read(buffer);

        // int result = mb.parse(buffer, length);
        int result = mb.response(buffer, length);
        printf("result=%d\n", result);
        Sleep(1000);
    }
    // Close serial port
    ser.Close();

    return 0;
}
